#include "mainwindow.h"

ToolBar::ToolBar(QWidget *p)
    :QToolBar(p)
{}

void ToolBar::paintEvent(QPaintEvent *)
{
    QLinearGradient gr(0, 0, width(), height());
    gr.setColorAt(0, QColor(128, 128, 128, 128));
    gr.setColorAt(0.5, QColor(255, 255, 255, 128));
    gr.setColorAt(1, QColor(128, 128, 128, 128));
    QPainter painter(this);
    painter.setBrush(gr);
    painter.drawRect(rect());
    painter.end();
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    central_wid = new QStackedWidget;
    user_search = new UserSearch;
    user_registration = new UserRegistration;
    connection_window = new ConnectionWindow;
    connect(connection_window, SIGNAL(connectionOk()), SLOT(enableLeftToolBarActions()));
    connect(connection_window, SIGNAL(connectionNotOk()), SLOT(disableLeftToolBarActions()));
    ToolBar *top_tool_bar = new ToolBar(this);
    top_tool_bar->setMovable(false);
    top_tool_bar->setIconSize(QSize(70, 70));
    addToolBar(top_tool_bar);
    db_backup_act = new QAction(this);
    db_backup_act->setIcon(QPixmap(":/icons/db_update.png"));
    db_backup_act->setToolTip(QString::fromLocal8Bit("Зробити backup бази даних"));
    db_backup_act->setEnabled(false);
    top_tool_bar->addAction(db_backup_act);
    QLabel *backup_inf_lab = new QLabel(QString::fromLocal8Bit("Зробити\nbackup\nбази даних"), this);
    top_tool_bar->addWidget(backup_inf_lab);
    QAction *settings_act = new QAction(this);
    settings_act->setIcon(QPixmap(":/icons/settings.png"));
    settings_act->setToolTip(QString::fromLocal8Bit("Налаштування"));
    settings_act->setCheckable(true);
    top_tool_bar->addAction(settings_act);
    QLabel *set_inf_lab = new QLabel(QString::fromLocal8Bit("Налаш-\nтування"), this);
    top_tool_bar->addWidget(set_inf_lab);
    connection_act = new QAction(this);
    connection_act->setIcon(QPixmap(":/icons/isync.png"));
    connection_act->setToolTip(QString::fromLocal8Bit("З'єднатися із сервером"));
    connection_act->setCheckable(true);
    connect(connection_act, SIGNAL(triggered()), SLOT(connectionSettings()));
    top_tool_bar->addAction(connection_act);
    QLabel *connect_inf_lab = new QLabel(QString::fromLocal8Bit("З'єднатися\nіз сервером"), this);
    top_tool_bar->addWidget(connect_inf_lab);
    QAction *close_act = new QAction(this);
    close_act->setIcon(QPixmap(":/icons/cancel.png"));
    close_act->setToolTip(QString::fromLocal8Bit("Вихід"));
    connect(close_act, SIGNAL(triggered()), qApp, SLOT(quit()));
    top_tool_bar->addAction(close_act);
    QLabel *close_inf_lab = new QLabel(QString::fromLocal8Bit("Вихід"), this);
    top_tool_bar->addWidget(close_inf_lab);
    ToolBar *left_tool_bar = new ToolBar(this);
    addToolBar(Qt::LeftToolBarArea, left_tool_bar);
    left_tool_bar->setMovable(false);
    left_tool_bar->setIconSize(QSize(70, 70));
    user_registration_act = new QAction(this);
    user_registration_act->setIcon(QPixmap(":/icons/new_user.png"));
    user_registration_act->setToolTip(QString::fromLocal8Bit("Додати клієнта"));
    user_registration_act->setEnabled(false);
    user_registration_act->setCheckable(true);
    connect(user_registration_act, SIGNAL(triggered()), SLOT(userRegistration()));
    left_tool_bar->addAction(user_registration_act);
    QLabel *registr_inf_lab = new QLabel(QString::fromLocal8Bit("Додати клієнта"), this);
    left_tool_bar->addWidget(registr_inf_lab);
    user_search_act = new QAction(this);
    user_search_act->setIcon(QPixmap(":/icons/search_client.png"));
    user_search_act->setToolTip(QString::fromLocal8Bit("Пошук клієнта"));
    user_search_act->setEnabled(false);
    user_search_act->setCheckable(true);
    connect(user_search_act, SIGNAL(triggered()), SLOT(userSearching()));
    left_tool_bar->addAction(user_search_act);
    QLabel *search_inf_lab = new QLabel(QString::fromLocal8Bit("Пошук клієнта"), this);
    left_tool_bar->addWidget(search_inf_lab);
    showMaximized();
    setWindowTitle("FreeRadiusUserSystemControl");
    central_wid->addWidget(new QLabel(QString::fromLocal8Bit("<center>Система керування користувачами "
                                                             "FreeRadiusUserSystemControl</center>")));
    central_wid->addWidget(user_registration);
    central_wid->addWidget(user_search);
    central_wid->addWidget(connection_window);
    setCentralWidget(central_wid);
}

MainWindow::~MainWindow()
{
    delete user_registration;
    delete connection_window;
    delete user_search;
    delete central_wid;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QLinearGradient gr(0, 0, width(), height());
    gr.setColorAt(0.0, QColor(0, 0, 0, 128));
    gr.setColorAt(1, QColor(255, 255, 255, 128));
    QPainter painter(this);
    painter.setBrush(gr);
    painter.drawRect(rect());
    painter.end();
}

void MainWindow::resizeEvent(QResizeEvent *)
{
    if (central_wid->currentWidget() == connection_window) __commonHandle(connection_window, connection_act, 3, 10, 3, height() / 2 + 200, false);
    else if (central_wid->currentWidget() == user_registration) __commonHandle(user_registration, user_registration_act, 4, 10, 4, 10, false);
    else if (central_wid->currentWidget() == user_search) __commonHandle(user_search, user_search_act, 0, 10, 0, 10, false);
}

void MainWindow::enableLeftToolBarActions()
{
    user_registration_act->setEnabled(true);
    user_search_act->setEnabled(true);
    db_backup_act->setEnabled(true);
    central_wid->setCurrentIndex(0);
    connection_act->setChecked(false);
}

void MainWindow::disableLeftToolBarActions()
{
    if (!user_registration_act->isEnabled()) return;
    user_registration_act->setEnabled(false);
    user_search_act->setEnabled(false);
    db_backup_act->setEnabled(false);
}

void MainWindow::connectionSettings()
{
   __commonHandle(connection_window, connection_act, 3, 10, 3, height() / 2 + 200);
}

void MainWindow::userRegistration()
{
    __commonHandle(user_registration, user_registration_act, 4, 10, 4, 10);
}

void MainWindow::userSearching()
{
    __commonHandle(user_search, user_search_act, 0, 10, 0, 10);
}

void MainWindow::__commonHandle(QWidget *wid, QAction *act, uint l_division, uint top, uint r_division, uint bottom, bool click_but)
{
    static QAction *current_act = 0;
    if ((central_wid->currentWidget() == wid) && click_but)
    {
        if (wid->isHidden()) wid->show();
        else
        {
            centralWidget()->setContentsMargins(0, 0, 0, 0);
            wid->hide();
            central_wid->setCurrentIndex(0);
            current_act = 0;
        }
    }
    else
    {
        if (click_but)
        {
            if (current_act && current_act->isChecked()) current_act->setChecked(false);
            current_act = act;
            central_wid->setCurrentWidget(wid);
        }
        uint left_space = 0, right_space = 0;
        if (l_division != 0) left_space = wid->width() / l_division/* - 70*/;
        if (r_division != 0) right_space = wid->width() / r_division;
        wid->setContentsMargins(left_space, top, right_space, bottom);
    }
}
